<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入班组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:management:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:management:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:management:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:management:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="班组编号" align="center" prop="teamNo" />
      <el-table-column label="班组名称" align="center" prop="teamName" />
      <el-table-column label="禁用标识" align="center" prop="teamStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.base_team_status" :value="scope.row.teamStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建角色" align="center" prop="createRole" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" key="teamStatus" :show-overflow-tooltip="false">
        <template slot-scope="scope">
              <el-switch
                v-model="scope.row.teamStatus"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              >
              
</el-switch>
            </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          
          <el-button type="text" size="small"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="bianji"
            v-hasPermi="['base:management:edit']">编辑</el-button>
          
          <el-button size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            :disabled="del"
            v-hasPermi="['base:management:remove']">删除</el-button>

        </template>
      </el-table-column>
      
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班组管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="班组编号" prop="teamNo">
          <el-input v-model="form.teamNo" placeholder="请输入班组编号" />
        </el-form-item> -->
        <el-form-item label="班组名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入班组名称" />
        </el-form-item>
        <el-form-item label="状态" prop="zhuangTai">
          <el-select v-model="form.teamStatus" placeholder="请选择禁用标识">
            <el-option
              v-for="dict in dict.type.base_team_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="人员" prop="phone">
          <el-select v-model="form.teamStatus" placeholder="请选择禁用标识">
            <el-option
              v-for="dict in dict.type.base_team_status"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- <el-dialog
  title="提示"
  :visible.sync="visible"
  width="30%"
  :before-close="handleClose">
  <span>这是一段信息</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="handleCancel">取 消</el-button>
    <el-button type="primary" @click="handle">确 定</el-button>
  </span>
</el-dialog> -->
  </div>
</template>

<script>
import { listManagement, getManagement, delManagement, addManagement, updateManagement,changeUserStatus } from "@/api/base/management";

export default {
  name: "Management",
  dicts: ['base_team_status'],
  data() {
    return {
      showSearch:'',
      visible:false,
      bianji:false,
      del:false,
      value:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      teamStatus: null,
      // 总条数
      total: 0,
      // 班组管理表格数据
      managementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamName: null,
        enableStatus:undefined
      },
      // 表单参数
      form: {},
      row:{},
      // 表单校验
      rules: {
        teamName: [
          { required: true, message: "班组编号不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "班组编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
//      handle(){
//        this.visible = false
//     console.log(this.row)
// changeUserStatus(this.row.id, this.row.teamStatus)
// this.getList();
//   },
//      handleCancel(){
//       this.visible = false
//     console.log(this.row)
//     // changeUserStatus(this.row.id, this.row.teamStatus)
//     this.getList();
//   },

//     handleClose(){
//       this.visible = false
//     },
    /** 查询班组管理列表 */
    getList() {
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      handleStatusChange(row){
    // let text = row.teamStatus === "0" ? "启用" : "禁用"
    // this.$confirm('确认要'+text + row.teamName+'吗','警告',{
    //   confirmButtonText:'确定',
    //   cancelButtonText:'取消',
    //   type:'warning'
    // }).then(function(){
    //   return isOn(row.id,row.teamStatus);
    // }).then(()=>{
    //   this.msgSuccess(text+"成功");
    //   console.log(row.teamStatus)
    // }).catch(function(){
    //   row.teamStatus = row.teamStatus === "0"?"1":"0";
    // })
    // this.visible=true
    // this.status =row.status === "0" ? "启用" : "禁用";
    //  this.teamName =  row.teamName + '"用户吗?', "警告";
    //  this.row = row
     changeUserStatus(row.id, row.teamStatus)   
      this.$message({
        
          showClose: true,
          message: row.teamStatus==0?'启用':'停用' ,
          type: row.teamStatus==0?'success':'warning'
        });

     setTimeout(() => {
      this.getList()
   
     }, 300);
      // this.$confirm('确认要"' + text + '""' + row.teamName + '"用户吗?', "警告", {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning"
      //   }).then(function() {
      //     return changeUserStatus(row.id, row.teamStatus);
      //   }).then(() => {
      //    this.$modal.msgSuccess(text+"成功");
      //   }).catch(function() {
      //     row.status = row.status === "0" ? "1" : "0";
      //   });
      // this.handle(row)
  },
 
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teamNo: null,
        teamName: null,
        teamStatus: null,
        createBy: null,
        createRole: null,
        createTime: null,
        remark: null,
        phone:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班组管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getManagement(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班组管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除班组管理编号为"' + ids + '"的数据项？').then(function() {
        return delManagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/management/export', {
        ...this.queryParams
      }, `management_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
